package com.neu.controller;

import com.neu.bean.User;
import com.neu.service.UserService;
import com.neu.utils.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by dllo on 18/3/12.
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping(value = {"/", "index",""})
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/login")
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/register")
    public String register() {
        return "register";
    }

    @ResponseBody
    @RequestMapping(value = "/reg")
    public AjaxResult register(@RequestParam("username") String uname, @RequestParam("password") String password, @RequestParam("tel") String tel) {
        System.out.println(uname+" "+password+""+ tel);
        return userService.reg(uname, password, tel);
    }
    @RequestMapping(value = "/log")
    public String log(@RequestParam("username") String name, @RequestParam("password") String password,HttpSession session) throws Exception {
        User user = userService.login(name, password);
        System.out.println("用户登录：" + name + password);
        if (user != null) {
            session.setAttribute("user", user.getUname());
            return "index";
        }
        return "fail";
    }
}
